%% Plotting HabNet v1 results

%close all
clc

timestep = 0.1;  % need to change this - make it not hardcoded??

%% construct time vector
time = 1:length(Hab_Vector_In_before_CCC.signals.values(:,2,:));


%% AR pressure

pressure = Hab_Vector_In_before_CCC.signals.values(:,2,:);
for i = 1:length(pressure)
    pressure_values(i) = pressure(:,:,i);
end

figure()

subplot(1,3,1)
plot(time*timestep/60/60,pressure_values, 'LineWidth',2);

title ('Air Reservoir pressure','FontSize', 16, 'FontWeight' , 'bold')
xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
ylabel('Pressure (kPa)','FontSize', 12, 'FontWeight' , 'bold')


%% AR temperature

temperature = Hab_Vector_In_before_CCC.signals.values(:,1,:);
for i = 1:length(temperature)
    temperature_values(i) = temperature(:,:,i);
end

%figure()
subplot(1,3,2)

plot(time*timestep/60/60,temperature_values, 'LineWidth',2)

title ('Air Reservoir temperature','FontSize', 16, 'FontWeight' , 'bold')
xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
ylabel('Temperature (K)','FontSize', 12, 'FontWeight' , 'bold')

%% AR mass

mass = Hab_Vector_In_before_CCC.signals.values(:,5,:);
for i = 1:length(mass)
    mass_values(i) = mass(:,:,i);
end

%figure()
subplot(1,3,3)

plot(time*timestep/60/60,mass_values, 'LineWidth',2)

title ('Air Reservoir mass','FontSize', 16, 'FontWeight' , 'bold')
xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
ylabel('Mass (kg)','FontSize', 12, 'FontWeight' , 'bold')


%% AR CO2, H2O and O2 composition

% CO2
CO2_mf = Hab_Vector_In_before_CCC.signals.values(:,6,:);
for i = 1:length(CO2_mf)
    CO2_mf_values(i) = CO2_mf(:,:,i);
end

% for i = 1:length(CO2_mf)
%     CO2_concentration(i) = CO2_mf_values(i) * mass_values(i);
% end

% O2
O2_mf = Hab_Vector_In_before_CCC.signals.values(:,7,:);
for i = 1:length(O2_mf)
    O2_mf_values(i) = O2_mf(:,:,i);
end

% for i = 1:length(O2_mf)
%     O2_concentration(i) = O2_mf_values(i) * mass_values(i);
% end

% H2O
H2O_mf = Hab_Vector_In_before_CCC.signals.values(:,8,:);
for i = 1:length(H2O_mf)
    H2O_mf_values(i) = H2O_mf(:,:,i);
end

% for i = 1:length(H2O_mf)
%     H2O_concentration(i) = H2O_mf_values(i) * mass_values(i);
% end

figure()
subplot(3,1,1), plot(time*timestep/60/60,CO2_mf_values, 'LineWidth',2)
title ('CO_2 mass fraction in Air Reservoir','FontSize', 16, 'FontWeight' , 'bold')
xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
ylabel('Mass Fraction','FontSize', 12, 'FontWeight' , 'bold')

subplot(3,1,2), plot(time*timestep/60/60,O2_mf_values, 'LineWidth',2)
title ('O_2 mass fraction in Air Reservoir','FontSize', 16, 'FontWeight' , 'bold')
xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
ylabel('Mass Fraction','FontSize', 12, 'FontWeight' , 'bold')

subplot(3,1,3), plot(time*timestep/60/60,H2O_mf_values, 'LineWidth',2)
title ('H_2O mass fraction in Air Reservoir','FontSize', 16, 'FontWeight' , 'bold')
xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
ylabel('Mass Fraction','FontSize', 12, 'FontWeight' , 'bold')



% subplot(3,1,1), plot(time*timestep/60/60,CO2_concentration, 'LineWidth',2)
% title ('CO2 concentration in Air Reservoir','FontSize', 16, 'FontWeight' , 'bold')
% xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
% ylabel('Concentration','FontSize', 12, 'FontWeight' , 'bold')
% 
% subplot(3,1,2), plot(time*timestep/60/60,O2_concentration, 'LineWidth',2)
% title ('O2 concentration in Air Reservoir','FontSize', 16, 'FontWeight' , 'bold')
% xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
% ylabel('Concentration','FontSize', 12, 'FontWeight' , 'bold')
% 
% subplot(3,1,3), plot(time*timestep/60/60,H2O_concentration, 'LineWidth',2)
% title ('H2O concentration in Air Reservoir','FontSize', 16, 'FontWeight' , 'bold')
% xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
% ylabel('Concentration','FontSize', 12, 'FontWeight' , 'bold')

%% O2 tank remaining mass

O2_tank_initial_mass = Tank_Output.signals.values(1,5,2);

current_mass = Tank_Output.signals.values(:,5,:);
for i = 1:length(current_mass)
    current_O2_mass(i) = current_mass(:,:,i);
end

figure()

subplot(1,2,1)

plot(time(2:end)*timestep/60/60,current_O2_mass(2:end)/O2_tank_initial_mass, 'LineWidth',2)
title ('Percent O_2 mass available in O_2 tank','FontSize', 16, 'FontWeight' , 'bold')
xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
ylabel('Percent available','FontSize', 12, 'FontWeight' , 'bold')

% need to configure y axis properly here, it is very close to 1 so we need
% a zoom on the area around 1

%% CCC remaining capacity

CCC_initial_capacity = CCC.signals.values(1,1,1);

mass_adsorbed = CCC.signals.values(:,2,:);
for i = 1:length(mass_adsorbed)
    mass_adsorbed1(i) = mass_adsorbed(:,:,i);
end

percent_capacity = (CCC_initial_capacity-mass_adsorbed1)/CCC_initial_capacity;

%figure()
subplot(1,2,2)

plot(time*timestep/60/60,percent_capacity, 'LineWidth',2)
title ('Percent CCC absorption capacity available','FontSize', 16, 'FontWeight' , 'bold')
xlabel('Time (hrs)','FontSize', 12, 'FontWeight' , 'bold')
ylabel('Percent available','FontSize', 12, 'FontWeight' , 'bold')

% need to configure y axis properly here, it is very close to 1 so we need
% a zoom on the area around 1


%% print the system's MPV

sum_matrix = params_Atmosphere_Providing_Tech_Choice1.signals.values + ...
params_Atmosphere_Providing_Tech_Choice2.signals.values + ...
params_CO2_Removing_Tech_Choice.signals.values + ...
params_Circulating_Air_Tech_Choice.signals.values + ...
params_Cooling_Condensing_Tech_Choice.signals.values + ...
params_O2_Storing_Tech_Choice.signals.values + ...
params_Pressure_Controlling_Tech_Choice1.signals.values + ...
params_Pressure_Controlling_Tech_Choice2.signals.values;

system_M = sum(sum_matrix(:,1));
system_P = sum(sum_matrix(:,2));
system_V = sum(sum_matrix(:,3));

fprintf('System Mass               = %2.10f kg\n',system_M);
fprintf('System Power Requirement  = %2.10f W\n',system_P);
fprintf('System Volume             = %2.10f  m^3\n',system_V);



